<?PHP

require_once 'entity/MetaObject.class.php';
require_once 'service/MetaObjectServiceImpl.class.php';

class ImportFileServiceImpl{

		private $html = null;
		private $metaObject = null;
		private $metaObjectServiceImpl = null;

		public function __construct(){
			$this->metaObjectServiceImpl = new MetaObjectServiceImpl();	
		}
		
		public function getMetaObjectServiceImpl(){
			return $this->metaObjectServiceImpl;
		}
		
		public function getNumerLineByFile($uriFile){
			$count = 0;
			$arquivo = fopen ($uriFile, 'r');
				while(!feof($arquivo)){
					$linha = fgets($arquivo, 1024);
					$count = $count + 1;
				}
			fclose($arquivo);
			return $count;
		}
		
		public function getHtmlListMetaObjectByFile($uriFile){
			$this->html = '';
			$this->html = $this->html.'<fieldset><table><tr><td>Nome</td><td>Label</td></tr>';
			$arquivo = fopen ($uriFile, 'r');
			while(!feof($arquivo)){
				$linha = fgets($arquivo, 1024);				
				list($campo_1,$campo_2) = preg_split ("/;/", $linha, 2);
				$this->html = $this->html.'<tr>';
				$this->html = $this->html.'<td>'.$campo_1.'</td>';
				$this->html = $this->html.'<td>'.$campo_2.'</td>';
				$this->html = $this->html.'</tr>';
			}
			// Fecha arquivo aberto
			fclose($arquivo);
			$this->html = $this->html.'</table></fieldset>';
			return $this->html;
		}
		
		public function importFile($uriFile,$language,$module,$client){
			$arquivo = fopen ($uriFile, 'r');
				while(!feof($arquivo)){
					$linha = fgets($arquivo, 1024);
					list($campo_1,$campo_2) = preg_split ("/;/", $linha, 2);

					$this->metaObject = new MetaObject();
					$this->metaObject->setName($campo_1);
					$this->metaObject->setCompleteLabel($campo_2);
					$this->metaObject->setLanguage($language);
					$this->metaObject->setModule($module);
					$this->metaObject->setClient($client);
					
					$this->metaObjectServiceImpl->insert($this->metaObject);
				}
			fclose($arquivo);
		}
}
?>